import {
  listColumns,
  tabArr
} from './index.const';
import {
  newspaperDateConfigList,
  queryOriginalFactoryList,
  delectNewspaperDateConfig,
  queryProductLineList
} from '@api/system_config_v1.js';
import ElEditable from '@components/el-editable';
import tabs from '@components/tabs/index.vue';

export default {
  components: {
    tabs,
    ElEditable
  },
  data() {
    return {
      // 多条件查询订单
      tableQuery: {
        newspaperDateConfigType: 1,
        originalFactoryCode: '',
        productLineCode: ''
      },
      newspaperDateConfigType: 1,
      tableLoading: false,
      listColumns: [], //表头
      //列表数据
      payload: {},
      loading: false,
      originalFactoryList: [],
      productLineList: [],
      // 主列表分页
      currentPage: 1,
      pageSize: 20,
      activeIndex: 0
    };
  },
  created() {
    this.handelQuery = false
    this.tabArr = tabArr
    this.activeIndex = this.$route.query.type === 'temporary' ? 1 : 0
    // newspaperDateConfigType 1-固定日期 2-临时日期
    this.newspaperDateConfigType
      = (this.$route.query.type === 'fixed' || !this.$route.query.type)
      ? 1 : 2
    this.setListColumns()
    this.getList();
  },
  methods: {
    setListColumns () {
      if (this.newspaperDateConfigType === 1) {
        this.listColumns = listColumns[0]
      } else {
        this.listColumns = listColumns[1]
      }
    },
    // 重置
    queryFormReset() {
      this.tableQuery = {};
      // this.tableQuery.newspaperDateConfigType
      //   = (this.$route.query.type === 'fixed' || !this.$route.query.type)
      //   ? 1 : 2
      this.setListColumns()
      this.currentPage = 1;
      this.handelQuery = false
      this.getList();
    },
    // 查询
    queryFormQuery(buttonClick) {
      this.currentPage = 1
      this.handelQuery = buttonClick
      this.getList()
    },
    handletabs(index) {
      if (!this.handelQuery) {
        this.tableQuery = {};
      }
      this.newspaperDateConfigType = index + 1
      this.setListColumns()
      this.queryFormQuery(this.handelQuery)
    },
    // 原厂下拉
    queryOriginalFactoryList (val) {
      this.loading = true;
      let query = {
        originalFactoryName: val,
      };
      this.$axios.$post(queryOriginalFactoryList, query).then(res => {
        this.originalFactoryList = res.payload;
        this.loading = false;
      });
    },
    // 产品线下拉
    queryProductLineList (val) {
      this.loading = true;
      let query = {
        productLineName: val,
        originalFactoryCode: this.tableQuery.originalFactoryCode
      };
      this.$axios.$post(queryProductLineList, query).then(res => {
        this.productLineList = res.payload;
        this.loading = false;
      });
    },
    // 主列表数据
    getList() {
      this.tableLoading = true;
      const query = {
        ...this.tableQuery,
        newspaperDateConfigType: this.newspaperDateConfigType,
        page: this.currentPage,
        pageSize: this.pageSize
      };
      this.$axios.$post(newspaperDateConfigList, query).then(res => {
        if (res.code === '0') {
          res.payload.content = res.payload.content || []
          this.payload = res.payload;
        }
        this.tableLoading = false;
      });
    },
    // 主列表分页器
    reloadProductData(val) {
      this.currentPage = val.number
      this.pageSize = val.size
      this.getList();
    },
    //表格选择数据
    selectionChange(val) {
    },
    onAdd (type) {
      this.$router.push({
        path: '/system-config/announcement-date-configuration/' + type
      })
    },
    onEdit (item) {
      const type = this.newspaperDateConfigType === 1
        ? 'fixed'
        : 'temporary'
      this.$router.push({
        path: '/system-config/announcement-date-configuration/' + type,
        query: {
          queryProductLineCode: item.productLineCode
        }
      })
    },
    onDelete (item) {
      this.$confirm('确定要删除该数据吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.$post(delectNewspaperDateConfig, {
          configSaleDateId: item.configSaleDateId
        }).then(res => {
          this.getList()
          this.$message.success('删除成功')
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  }
};
